[mxfp8 training] fused gated-activation (SwiGLU) + MXFP8 quantization kernel override - #2
Closed
wolfcomos wants to merge 4 commits into
Closed
[mxfp8 training] fused gated-activation (SwiGLU) + MXFP8 quantization kernel override #2wolfcomos wants to merge 4 commits into
wolfcomos wants to merge 4 commits into
Conversation
…sedSwiGLU Integrates the TorchAO unified SwiGLU+MXFP8 CuTe DSL composite (mxfp8_swiglu_mlp_w13 / mxfp8_swiglu_grouped_mlp_w13) behind a new fuse_swiglu_mxfp8 config flag, with A/B debug configs for Llama3 (llama3_debugmodel_mxfp8[_fused_swiglu]) and DeepSeek-V3 (deepseek_v3_debugmodel_mxfp8[_fused_swiglu]). Both arms share the composite autograd boundary; only fuse_activation differs. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Mirrors the JET regular-MXFP8 workload (MXFP8Linear over attention, dense FFN and shared-expert linears, plus MXFP8 routed-expert grouped GEMMs) so the SwiGLU fusion can be measured end to end rather than only in a microbenchmark. Three arms, because the fused-w13 override and the unified SwiGLU+MXFP8 kernel are independent changes and move in opposite directions: deepseek_v3_16b_mxfp8_exp no override (the JET control) deepseek_v3_16b_mxfp8_w13_exp fused w13 GEMM, standalone casts deepseek_v3_16b_mxfp8_fused_swiglu_exp fused w13 + unified kernel Measured on 4x GB200 (EP=4, bs=4, seq=4096, 50 steps), averaged over steps 11-50: the w13 override alone is -0.51% TFLOPs and +3.3GiB, the unified kernel adds +2.51% on top of it, for +1.98% against the control. Loss at step 50 agrees to within 1.5e-3 across all three. Uses the in-repo test tokenizer and c4_test so the configs run without downloaded assets; token content affects neither FLOPs nor kernel shapes. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Moved from torchao (which now offers only the fused gated-activation kernels): the autograd composite, its tests, and the FusedSwiGLU import sites. No functional change; 34 composite + 18 override tests pass. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
torchao moved the fused gated-activation custom ops out of quant.py into
the kernel module and renamed them with the cutedsl infix
(gated_act_mxfp8_cutedsl_{forward,backward}). The composite now imports
the wrappers lazily at the two fused-path call sites -- the kernel module
imports the CuTe DSL runtime at module scope, and the unfused fallback
must keep working without it. Trace-count assertions updated to the new
op names.
52/52 swiglu unit tests green against torchao 39db5297.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
vkuzo
reviewed
Aug 19, 2026
| dgrad activation casts, while the BF16 activation values needed by the two | ||
| weight-gradient GEMMs are recomputed from the saved gated projection. Shapes | ||
| the CuTeDSL kernels cannot handle still fall back to the existing per-GEMM | ||
| ``mx_mm`` path (or plain BF16 as a last resort) instead of asserting. |
There was a problem hiding this comment.
i'd advocate against falling back to bf16 based on shape, IMO better to honor the user config and let the user change the config if it cannot be executed
Owner
Author
There was a problem hiding this comment.
Thanks a lot for the review @vkuzo ! I addressed this and it's now updated at pytorch#4257.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds an opt-in override module,
torchtitan/overrides/mxfp8_fused_swiglu.py, that runs aFeedForward / routed-experts MLP entirely in MXFP8 with a fused SwiGLU boundary: one composite
autograd function computes
w13 GEMM -> fused SwiGLU + dual MXFP8 quantization -> w2 GEMM(dense and grouped variants), using torchao's fused gated-activation kernel
(pytorch/ao#4743) for the activation boundary and torchao's MXFP8 grouped/scaled-mm internals
for the GEMMs. Compared to the unfused MXFP8 path, the fused boundary eliminates the standalone
activation-quantization casts (fused mode launches 1 fwd + 1 bwd fused op instead of 6 + 6
standalone cast kernels per MLP) and never materializes the BF16 post-activation tensor.
The integration contained:
MXFP8FusedSwiGLU(FusedSwiGLU)andMXFP8FusedGroupedExperts(FusedGroupedExperts)subclassthe existing fused modules (inheriting the
w13parameter, stock-layout checkpoint hooks, andinit/sharding remaps) and override only
forward.@overridefactories:mxfp8_fused_swiglu(targetsFeedForward.Config) andmxfp8_fused_grouped_experts(targetsRoutedExperts.Config, which owns both the tokendispatcher and the inner experts — the factory swaps the dispatcher to the padded variant its
kernels require,
pad_multiple=128).overrides/fused_swiglu.py,components/quantization/mx.py, and the quantization converters are untouched. Enabling theoverride is the only opt-in; no converter or job-config plumbing is added.
Fail-loud contract
There is no silent fallback anywhere in this path. Unsupported configurations raise actionable
errors naming the offending value (unsupported SM at override-application time; non-%128 dims,
non-BF16 dtypes, DTensor activations, and misaligned token counts at call time — data-dependent
token counts use
torch._checkdeferred asserts so routing-dependent shapes work undertorch.compile). The user changes the config; the code never substitutes a different numericspath than the one configured.
Usage
Example flavors:
llama3_debugmodel_mxfp8_fused_swiglu,deepseek_v3_debugmodel_mxfp8_fused_swiglu(both enable compile; attention/lm_head stay BF16 — composable with
MXFP8LinearConverteronthose modules independently).
Numerics and tests
tests/unit_tests/test_mxfp8_swiglu_override.py— CPU-runnable wiring suite(config-tree transform,
apply_overrides, meta-device build, dispatcherpad_multiple,factory fail-loud), validated both with GPUs visible and with
CUDA_VISIBLE_DEVICES="".(upstream CI has no SM100): 25-test suite on GB200 covering bitwise forward parity between the
fused kernel and standalone-cast quantization, SQNR tracking against the per-GEMM MXFP8 and
BF16 references,
torch.compilewith unbacked routing-dependent token counts, dispatcherpad-row/tail inertness, and profiler op-count contracts.
caused to the kernel's correctly-rounded sigmoid + FMA contraction) are documented in
[mxfp8 training] Add a fused gated-activation (SwiGLU) + MXFP8 quantization kernel pytorch/ao#4743.